// ITI 1120 Fall 2012, Lab 4, Exercise 3 // Name: Morvarid Sehatkar, Student# 123456 /** * Generates numbers in Fibonacci series up to a positive integer n **/ class Lab4Ex3 { public static void main (String args[ ]) { // DECLARE VARIABLES/DATA DICTIONARY int n; // Fibonacci series will be computed up to n char answer; //y/n answer from user to continue // PRINT OUT IDENTIFICATION INFORMATION System.out.println("ITI 1120 Fall 2012, Lab 4, Exercise 3"); System.out.println("Name: Grace Hoper, Student# 12345678"); System.out.println(); do { // READ IN GIVENS use our ITI1120 special class for keyboard input // none to read. System.out.print("Please give a positive integer as the maximum value in Fibonacci series: "); n = ITI1120.readInt(); // Reads integer number from the keyboard // BODY OF ALGORITHM - Call to the method to solve problem fibonacci(n); //Ask the user for another run System.out.println(); System.out.print("Do you wish another run(y/n)? "); answer = ITI1120.readChar(); } while(answer == 'y' || answer == 'Y'); System.out.println(); System.out.print("Program terminated!"); } // Problem Solving Method // Description: The Fibonacci series // Parameters (GIVENS): num - the max value in Fibonacci series public static void fibonacci(int num) { // DECLARE VARIABLES/DATA DICTIONARY // INTERMEDIATES // RESULT int prevNum; // Previous number in the series int curNum; // Current number in the series int nextNum; // Next number in the series // BODY OF ALGORITHM // Inializer variables prevNum = 0; curNum = 1; //Print the first and the second number in the series System.out.println("Fibonacci series up to " + num + ": "); System.out.print(prevNum + ", " + curNum); //Compute the next number nextNum = prevNum + curNum; while(nextNum <= num) { System.out.print(", " + nextNum); prevNum = curNum; curNum = nextNum; nextNum = prevNum + curNum; } } } // Don't remove this brace bracket!